// START WRAPPER: The YUI.add wrapper is added by the build system, when you use YUI Builder to build your component from the raw source in this file
 YUI.add("menu-html-creator", function(Y) {

    /* Any frequently used shortcuts, strings and constants */
    var Lang = Y.Lang;

    /* MenuHtmlCreator class constructor */
    function MenuHtmlCreator(config) {
        MenuHtmlCreator.superclass.constructor.apply(this, arguments);
    }

    /* 
     * Required NAME static field, to identify the class and 
     * used as an event prefix, to generate class names etc. (set to the 
     * class name in camel case). 
     */
    MenuHtmlCreator.NAME = "MenuHtmlCreator";
    
    
    MenuHtmlCreator.MENU_ITEM_CONTENT_TEMPLATE = '<a class="yui3-menuitem-content" href="{identity}">{title}</a>';
    
    /*
     * The attribute configuration for the component. This defines the core user facing state of the component
     */
    MenuHtmlCreator.ATTRS = {
    	menuItems : {
    		value : []
    	},
    	horizontal : {
    		value : true
    	},
        attrA : {
            value: "A"                     // The default value for attrA, used if the user does not set a value during construction.

            /*
            , valueFn: "_defAttrAVal"      // Can be used as a substitute for "value", when you need access to "this" to set the default value.
             
            , setter: "_setAttrA"          // Used to normalize attrA's value while during set. Refers to a prototype method, to make customization easier
            , getter: "_getAttrA"          // Used to normalize attrA's value while during get. Refers to a prototype method, to make customization easier
            , validator: "_validateAttrA"  // Used to validate attrA's value before updating it. Refers to a prototype method, to make customization easier

            , readOnly: true               // Cannot be set by the end user. Can be set by the component developer at any time, using _set
            , writeOnce: true              // Can only be set once by the end user (usually during construction). Can be set by the component developer at any time, using _set
            
            , lazyAdd: false               // Add (configure) the attribute during initialization. 
            
                                           // You only need to set lazyAdd to false if your attribute is
                                           // setting some other state in your setter which needs to be set during initialization 
                                           // (not generally recommended - the setter should be used for normalization. 
                                           // You should use listeners to update alternate state). 

            , broadcast: 1                 // Whether the attribute change event should be broadcast or not.
            */
        }

        // ... attrB, attrC, attrD ... attribute configurations. 

        // Can also include attributes for the super class if you want to override or add configuration parameters
    };

    /* MenuHtmlCreator extends the Base class */
    Y.extend(MenuHtmlCreator, Y.Base, {

        initializer: function() {
            /*
             * initializer is part of the lifecycle introduced by 
             * the Base class. It is invoked during construction,
             * and can be used to setup instance specific state or publish events which
             * require special configuration (if they don't need custom configuration, 
             * events are published lazily only if there are subscribers).
             *
             * It does not need to invoke the superclass initializer. 
             * init() will call initializer() for all classes in the hierarchy.
             */
        	this.menuHtml = '';
        	this.firstUl = true;

        },

        destructor : function() {
            /*
             * destructor is part of the lifecycle introduced by 
             * the Base class. It is invoked when destroy() is called,
             * and can be used to cleanup instance specific state.
             *
             * It does not need to invoke the superclass destructor. 
             * destroy() will call initializer() for all classes in the hierarchy.
             */
        },

        /* MenuHtmlCreator specific methods */
        
        
        getMenuHtml : function(mid){
        	if(!mid){
        		this._buildAMenu(-1);
        	}else{
        		this._buildAMenu(mid);
        	}
        	
        	return this.menuHtml;
        },
        
//        childrenNum
//    	0
//    	
//    createdAt
//    	"2012-03-21T14:05:02"
//    	
//    hidden
//    	false
//    	
//    id
//    	75803
//    	
//    keyValues
//    	"x=7,u=7"
//    	
//    linkTo
//    	""
//    	
//    lurl
//    	""
//    	
//    modelId
//    	0
//    	
//    modelName
//    	"com.m3958.firstgwt.server.model.Link"
//    	
//    name
//    	"大堰新闻"
//    	
//    parentId
//    	-1
//    	
//    parentIds
//    	""
//    	
//    position
//    	10
//    	
//    siteId
//    	74067
//    	
//    tplName
//    	""
//    	
//    url
//    	""
    
        _buildAMenu : function(pid){
        	var mis = this.get('menuItems'),
        		pitem = Y.Array.find(mis,function(item){
        			if(item.id == pid){
        				return true;
        			}
        		}),
        		children = Y.Array.filter(mis,function(mi){
        			if(mi.parentId == pid){
        				return true;
        			}
        		});
        		if(children.length > 0){//嵌套目录
        			if(pitem){
        				this.menuHtml += Y.Lang.sub('<li><a class="yui3-menu-label" href="#{mid}">{lname}</a>',{mid: 'm3958-menu-' + pid,lname:pitem.name});
        				this.menuHtml +=  Y.Lang.sub('<div id="{mid}" class="yui3-menu"><div class="yui3-menu-content"><ul>',{mid:'m3958-menu-' + pid});
        			}else{
        				if(this.get('horizontal')){
        					if(this.firstUl){
        						this.menuHtml +=  Y.Lang.sub('<div id="{mid}" class="yui3-menu yui3-menu-horizontal"><div class="yui3-menu-content"><ul class="first-of-type">',{mid:'m3958-menu-' + pid});
        					}else{
        						this.menuHtml +=  Y.Lang.sub('<div id="{mid}" class="yui3-menu yui3-menu-horizontal"><div class="yui3-menu-content"><ul>',{mid:'m3958-menu-' + pid});
        					}
        				}else{
        					if(this.firstUl){
        						this.menuHtml +=  Y.Lang.sub('<div id="{mid}" class="yui3-menu"><div class="yui3-menu-content"><ul class="first-of-type">',{mid:'m3958-menu-' + pid});
        					}else{
        						this.menuHtml +=  Y.Lang.sub('<div id="{mid}" class="yui3-menu"><div class="yui3-menu-content"><ul>',{mid:'m3958-menu-' + pid});
        					}
        				}
        				this.firstUl = false;
        			}
        			
        			Y.Array.each(children,function(it){
        				this._buildAMenu(it.id);
        			},this);
        			this.menuHtml +=  '</ul></div></div>';
        		}else{
        			if(pitem){
        				this.menuHtml += Y.Lang.sub('<li class="yui3-menuitem"><a class="yui3-menuitem-content" href="{mid}">{lname}</a></li>',{lname:pitem.name,mid:'#m3958-menu-item-'+ pid});
        			}
        		}
        },
        doSomethingPublic : function() {
            // Public method
        },

        _doSomethingPrivate : function() {
            // Private/Protected method
        },

        _defMyEventFn : function(e) {
            // The default behavior for the "myEvent" event.
        }
    });

    Y.namespace("M3958").MenuHtmlCreator = MenuHtmlCreator;

 }, "0.0.1", {requires:["base","event","array-extras"]});

 
